<?php

namespace api\controller;

use common\controller\VerifyController;
use common\service\EntityEpisodeRelationService;
use common\service\FixDataService;
use think\Exception;
use think\Request;
use think\Response;

class FixDataController extends VerifyController
{
    private $fixDataService;
    private $entityEpisodeRelationService;

    public function __construct()
    {
        parent::__construct();
        $this->fixDataService = FixDataService::getInstance();
        $this->entityEpisodeRelationService = EntityEpisodeRelationService::getInstance();
    }

    /**
     * 重写工序标准目录和文档
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function stepStandardsData(Request $request)
    {
        $res = $this->fixDataService->stepStandardsData();
        return json(success_response('ok', $res));
    }

    /**
     * 修复实际审核截至时间
     * @param Request $request
     * @return Response
     */
    public function fixPlaylistVersionEndTime(Request $request)
    {
        $res = $this->fixDataService->fixPlaylistVersionEndTime();
        return json(success_response('ok', $res));
    }

    /**
     * 填充项目对应的角色
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function fixProjectMemberRole(Request $request)
    {
        $data = $request->param()['data'];
        $res = $this->fixDataService->fixProjectMemberRole($data['role_code']);
        return json(success_response('ok', $res));
    }

    /**
     * 填充工单用户数据
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function fixTaskWorkOrderUser(Request $request)
    {
        $res = $this->fixDataService->fixTaskWorkOrderUser();
        return json(success_response('ok', $res));
    }

    /**
     * 订单实体信息修复
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function orderTaskEntityCheck(Request $request)
    {
        $res = $this->fixDataService->orderTaskEntityCheck($request->param()['order_id']);
        return json(success_response('ok', $res));
    }

    /**
     * 将工单的帖子转入到消息中,转换为会话逻辑
     * @return Response
     * @throws Exception
     */
    public function fixWorkNoteToMessage()
    {
        $res = $this->fixDataService->fixWorkNoteToMessage();
        return json(success_response('ok', $res));
    }

    /**
     * 填充工单关联任务信息
     * @return Response
     * @throws \Exception
     */
    public function fixWorkOrderTask()
    {
        $res = $this->fixDataService->fixWorkOrderTask();
        return json(success_response('ok', $res));
    }

    /**
     * 修复项目部分成员错误的数据
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function fixProjectMemberTenant(Request $request)
    {
        $res = $this->fixDataService->fixProjectMemberTenant($request->param()['project_id']);
        return json(success_response('ok', $res));
    }

    /**
     * 修复清单的外包状态
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function fixEntityIsOutsourceStatus(Request $request)
    {
        $isAsync = $request->param()['param']['is_async'] ?? 'no';
        $res = $this->fixDataService->fixEntityIsOutsourceStatus($request->param()['param']['filter'], $isAsync === 'yes');
        return json(success_response('ok', $res));
    }

    /**
     * 填充实体群聊
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function fixEntityChat(Request $request)
    {
        $res = $this->fixDataService->fixEntityChat($request->param()['param']);
        return json(success_response('ok', $res));
    }

    /**
     * 修正实体工序分类任务状态
     * @param Request $request
     * @return Response
     */
    public function fixEntityStepCategoryStatus(Request $request)
    {
        $res = $this->fixDataService->fixEntityStepCategoryStatus($request->param()['param']);
        return json(success_response('ok', $res));
    }

    /**
     * 修复工序任务计划时间
     * @return Response
     */
    public function fixStepCategoryTaskPlanTime(Request $request)
    {
        $res = $this->fixDataService->fixStepCategoryTaskPlanTime($request->param()['param']['filter']);
        return json(success_response('ok', $res));
    }

    /**
     * 修复工时记录
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function fixWorkTimeRecord(Request $request)
    {
        $res = $this->fixDataService->fixWorkTimeRecord($request->param()["param"]);
        return json(success_response('ok', $res));
    }

    /**
     * 镜头的剧集关系同步到资产
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function syncShotEpisodeRelationToAsset(Request $request)
    {
        $res = $this->entityEpisodeRelationService->syncShotEpisodeRelationToAsset($request->param()['param']['filter']);
        return json(success_response('ok', $res));
    }

    /**
     * 刷新剧集状态
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function flushEpisodeStatus(Request $request)
    {
        $res = $this->fixDataService->flushEpisodeStatus($request->param()['param']['filter']);
        return json(success_response('ok', $res));
    }

    /**
     * 刷新任务剧集id
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function flushTaskEpisodeIds(Request $request)
    {
        $page = $request->param()['param']['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->fixDataService->flushTaskEpisodeIds($request->param()['param']['filter'], $page);
        return json(success_response('ok', $res));
    }

    /**
     * 补齐计划代办任务以及标签
     * @param \think\Request $request
     * @return \think\Response
     * @throws \Throwable
     * @throws \think\Exception
     */
    public function fixPlanTodoTaskIdAndTag(Request $request)
    {
        $res = $this->fixDataService->fixPlanTodoTaskIdAndTag();
        return json(success_response('ok', $res));
    }

    /**
     * 修复实体分类任务开始结束时间
     * @param Request $request
     * @return \think\Response
     */
    public function fixEntityStepCategoryStartEndTime(Request $request)
    {
        $res = $this->fixDataService->fixEntityStepCategoryStartEndTime($request->param()['param']);
        return json(success_response('ok', $res));
    }

    /**
     * 修复工序任务成员信息
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function fixStepTaskMember(Request $request)
    {
        $res = $this->fixDataService->fixStepTaskMember($request->param()['param']);
        return json(success_response('ok', $res));
    }

    /**
     * 修复剧集关联实体
     * @param \think\Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function fixEpisodeRelationEntity(Request $request)
    {
        $res = $this->fixDataService->fixEpisodeRelationEntity($request->param()['param']);
        return json(success_response('ok', $res));
    }

    /**
     * 修复项目成员记录重复问题
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function fixProjectMemberDuplicateData(Request $request)
    {
        $filter = $request->param()['param']['filter'] ?? [];
        $res = $this->fixDataService->fixProjectMemberDuplicateData($filter);
        return json(success_response('ok', $res));
    }

    /**
     * 填充历史数据的交付时间
     * @param Request $request
     * @return Response
     */
    public function fillDefaultPlanDeliveryTime(Request $request)
    {
        $filter = $request->param()['param']['filter'];
        $page = $request->param()['param']['page'];
        $res = $this->fixDataService->fillDefaultPlanDeliveryTime($filter, $page);
        return json(success_response('ok', $res));
    }

    /**
     * 修改渲染工序到灯光环节
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function changeRenderToLighting(Request $request)
    {
        $filter = $request->param()['param']['filter'] ?? [];
        $page = $request->param()['param']['page'] ?? [1, 500];
        $res = $this->fixDataService->changeRenderToLighting($filter, $page);
        return json(success_response('ok', $res));
    }

    /**
     * 填充历史延期单环节确认数据
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function fillDelayOrderStepCategoryConfirm(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $filter = $param['filter'] ?? [];
        $page = get_default_page($param);
        $res = $this->fixDataService->fillDelayOrderStepCategoryConfirm($filter, $page);
        return json(success_response('ok', $res));
    }

    public function fixDemandOrderPlanDeliveryTime(Request $request)
    {
        $res = $this->fixDataService->fixDemandOrderPlanDeliveryTime();
        return json(success_response('ok', $res));
    }

    /**
     * 修复需求单子级计划交付任务
     * @param Request $request
     * @return Response
     */
    public function fixDemandOrderChildrenStepCategoryPlanDeliveryTime(Request $request)
    {
        $stepCategoryId = $request->param()['param']['filter']['step_category_id'];
        $res = $this->fixDataService->fixDemandOrderChildrenStepCategoryPlanDeliveryTime($stepCategoryId);
        return json(success_response('ok', $res));
    }

    /**
     * 同步任务上游IDs
     * @param Request $request
     * @return Response
     */
    public function fixSyncTaskUpstream(Request $request)
    {
        $param = $request->param() ?? [];
        $res = $this->fixDataService->fixSyncTaskUpstream($param);
        return json(success_response('ok', $res));
    }

    /**
     * 修复原计划节点数据
     * @param Request $request
     * @return Response
     */
    public function fixOriginalPlanNodeData(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $res = $this->fixDataService->fixOriginalPlanNodeData($param);
        return json(success_response('ok', $res));
    }

    /**
     * 修复原计划节点--补充旧数据
     * @param Request $request
     * @return Response
     */
    public function fixOriginalPlanNodeOldData(Request $request)
    {
        $param = $request->param()['param'] ?? [];
        $res = $this->fixDataService->fixOriginalPlanNodeOldData($param);
        return json(success_response('ok', $res));
    }

}
